package ModelLayer;
import java.util.ArrayList;


/**
 * Write a description of class CustomerCon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CustomerCon
{
    private static CustomerCon instance;
    private ArrayList<Customer> customerCon;
    private ArrayList<BusinessCustomer> businessCon;
    
    public static CustomerCon getInstance()
    {
        if(instance == null)
        {
            instance = new CustomerCon();
        }
        return instance;
    }
    
    private CustomerCon()
    {
        customerCon = new ArrayList<Customer>();
        businessCon = new ArrayList<BusinessCustomer>();
    }
    
    public Customer getCustomer(int id)
    {
        boolean found = false;
        int i = 0;
        Customer tempCustomer = null;
        while(i < customerCon.size() && !found)
        {
            tempCustomer = customerCon.get(i);
            if(tempCustomer.getId() == id)
            {
                found = true;
            }
            else
            {
                tempCustomer = null;
                i ++;
            }
        }
        return tempCustomer;
    }
    
    public BusinessCustomer getBusiness(int id)
    {
        boolean found = false;
        int i = 0;
        BusinessCustomer tempBusiness = null;
        while(i < businessCon.size() && !found)
        {
            tempBusiness = businessCon.get(i);
            if(tempBusiness.getId() == id)
            {
                found = true;
            }
            else
            {
                tempBusiness = null;
                i ++;
            }
        }
        return tempBusiness;
    }
    
    public ArrayList<Customer> getCustomerCon()
    {
        return customerCon;
    }
    
    public ArrayList<BusinessCustomer> getBusinessCustomer()
    {
        return businessCon;
    }
    
    public void printCustomer(int id)
    {
        if(getCustomer(id) != null)
        {
            System.out.println("ID: "+getCustomer(id).getId());
            System.out.println("Name: "+getCustomer(id).getName());
            System.out.println("Address: "+getCustomer(id).getAddress());
            System.out.println("Phone: "+getCustomer(id).getPhone());
            System.out.println("Mail: "+getCustomer(id).getMail());
            System.out.println("CPR number: "+getCustomer(id).getCPRnr()+"\n");
        }
        else
        {
            System.out.println("Matching customer not found for id: "+id);
        }
    }
    
    public void printBusiness(int id)
    {
        if(getBusiness(id) != null)
        {
            System.out.println("ID: "+getBusiness(id).getId());
            System.out.println("Name: "+getBusiness(id).getName());
            System.out.println("Address: "+getBusiness(id).getAddress());
            System.out.println("Phone: "+getBusiness(id).getPhone());
            System.out.println("Mail: "+getBusiness(id).getMail());
            System.out.println("Company name: "+getBusiness(id).getCompanyName());
            System.out.println("CVR number: "+getBusiness(id).getCVRnr());
            System.out.println("Business customer discount: "+getBusiness(id).getDiscount()+"\n");
        }
        else
        {
            System.out.println("Matching business customer not found for id: "+id);
        }
    }
    
    public void printCustomerList()
    {
        if(!customerCon.isEmpty())
        {
            System.out.println("List of customers: \n");
            for(int i = 0; i < customerCon.size(); i++)
            {
                System.out.println("ID: "+customerCon.get(i).getId());
                System.out.println("Name: "+customerCon.get(i).getName());
                System.out.println("Address: "+customerCon.get(i).getAddress());
                System.out.println("Phone: "+customerCon.get(i).getPhone());
                System.out.println("Mail: "+customerCon.get(i).getMail());
                System.out.println("CPR number: "+customerCon.get(i).getCPRnr()+"\n");
            }
        }
        else
        {
            System.out.println("There are currently no customers listed in the system");
        }
    }
    
    public void printBusinessList()
    {
        if(!businessCon.isEmpty())
        {
            System.out.println("List of business customers: \n");
            for(int i = 0; i < businessCon.size(); i++)
            {
                System.out.println("ID: "+businessCon.get(i).getId());
                System.out.println("Name: "+businessCon.get(i).getName());
                System.out.println("Address: "+businessCon.get(i).getAddress());
                System.out.println("Phone: "+businessCon.get(i).getPhone());
                System.out.println("Mail: "+businessCon.get(i).getMail());
                System.out.println("Company name: "+businessCon.get(i).getCompanyName());
                System.out.println("CVR number: "+businessCon.get(i).getCVRnr());
                System.out.println("Business discount: "+businessCon.get(i).getDiscount()+"\n");
            }
        }
        else
        {
            System.out.println("There are currently no business customers listed in the system");
        }
    }
    
    public void addCustomer(Customer customer)
    {
        customerCon.add(customer);
    }
    
    public void addBusiness(BusinessCustomer business)
    {
        businessCon.add(business);
    }
    
    public void removeCustomer(int id)
    {
        Customer tempCustomer = getCustomer(id);
        if(tempCustomer != null)
        {
            customerCon.remove(tempCustomer);
        }
        else
        {
            System.out.println("Matching customer not found for id: "+id);
        }
    }
    
    public void removeBusinessCustomer(int id)
    {
        BusinessCustomer tempBusiness = getBusiness(id);
        if(tempBusiness != null)
        {
            businessCon.remove(tempBusiness);
        }
        else
        {
            System.out.println("Matching business customer not found for id: "+id);
        }
    }
}
